package com.zxjbyte.yiyi.server.controller.demo;

import cn.hutool.core.thread.ThreadUtil;
import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.common.domain.api.R;
import com.zxjbyte.yiyi.framework.common.util.JsonUtil;
import com.zxjbyte.yiyi.framework.operatelog.annotation.OperateLog;
import com.zxjbyte.yiyi.framework.operatelog.enums.OperateTypeEnum;
import com.zxjbyte.yiyi.server.domain.request.demo.DemoGetParamRequest;
import com.zxjbyte.yiyi.server.domain.request.demo.DemoPostBodyRequest;
import com.zxjbyte.yiyi.server.domain.request.demo.DemoPostFormRequest;
import com.zxjbyte.yiyi.server.domain.request.demo.DemoPostValidRequest;
import com.zxjbyte.yiyi.server.service.demo.IDemoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 仅用于演示
 *
 * @Author zhangxingjia
 * @Date 2023/3/1 17:59
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "demo")
@RestController
@RequestMapping(value = Constant.BASE_PATH_B + "/demo")
public class DemoController {

    @Resource
    private IDemoService demoService;

    // ===================GET===================

    @Operation(summary = "Get方式Param")
    @Parameters({
            @Parameter(name = "id", description = "唯一id" ),
            @Parameter(name = "name", description = "名称" )
    })
    @GetMapping("/getParam")
    public R<?> demoGetParam(@RequestParam("id") Long id, @RequestParam("name") String name) {
        return R.data(demoService.demoGetParam(id, name));
    }

    @Operation(summary = "Get方式Path")
    @Parameter(name = "id", description = "唯一id" )
    @GetMapping("/getPath/{id:\\d+}")
    public R<?> demoGetPath(@PathVariable Long id){
        // 采用{id: } 方式校验参数，如果不匹配将会报404
        System.out.println("进行处理业务：" + id + " ====");
        ThreadUtil.sleep(1000);
        return R.ok();
    }

    @Operation(summary = "Get方式Object")
    @GetMapping("/getObject")
    public R<?> demoGetObject(@ParameterObject DemoGetParamRequest request){
        System.out.println("进行处理业务: " + JsonUtil.toJsonAsString(request));
        ThreadUtil.sleep(1000);
        return R.ok();
    }

    // ===================POST===================

    @Operation(summary = "Post方式JSON")
    @PostMapping("/postJson")
    @OperateLog(type = OperateTypeEnum.CREATE)
    public R<?> demoPost(@RequestBody DemoPostBodyRequest request){
        return R.data(request);
    }

    @Operation(summary = "Post方式Form表单")
    @PostMapping("/postForm")
    public R<?> demoPostForm(@ParameterObject @Valid DemoPostFormRequest request){
        return R.data(request);
    }

    @Operation(summary = "Post方式JSON参数校验")
    @PostMapping("/postJsonValid")
    public R<?> demoPostValid(@Valid @RequestBody DemoPostValidRequest request){
        return R.data(request);
    }


    // ===================DELETE===================

    @Operation(summary = "Delete方式")
    @Parameter(name = "id", description = "唯一id" )
    @DeleteMapping("/del")
    public R<?> demoDelete(@RequestParam("id") Long id) {
        return R.data(id);
    }
}
